package com.hxzy.service;

import com.baizhi.common.vo.PageVO;
import com.hxzy.dto.GoodsInfoSearch;
import com.hxzy.entity.GoodsInfo;
import com.hxzy.repository.GoodsInfoRepository;
import com.hxzy.vo.GoodsInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsInfoService {

    @Autowired
    private GoodsInfoRepository  goodsInfoRepository;

    /**
     * 新增
     * @param goodsInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(GoodsInfo goodsInfo){
        goodsInfo.setCreateTime(new Date());
        goodsInfo.setCreateUser(1);
        this.goodsInfoRepository.save(goodsInfo);
        return true;
    }

    /**
     * 修改
     * @param goodsInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(GoodsInfo goodsInfo){
        GoodsInfo db=this.goodsInfoRepository.getOne(goodsInfo.getGoodsId());
        if(db==null){
            return false;
        }

        BeanUtils.copyProperties(goodsInfo,db,"goodsId","createTime");
        db.setUpdateTime(new Date());
        //保存
        this.goodsInfoRepository.save(db);

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public GoodsInfo findById(Long goodsId){
        return this.goodsInfoRepository.getOne(goodsId);
    }


    @Transactional(rollbackFor = Exception.class)
    public List<GoodsInfoVO> findByName(String name) {
      List<GoodsInfo>  arr=this.goodsInfoRepository.findByGoodsNameLike(name);

      //自己来转
      List<GoodsInfoVO> arrVO=new ArrayList<>();
      for(GoodsInfo  goodsInfo : arr){
          GoodsInfoVO vo=new GoodsInfoVO();
          BeanUtils.copyProperties(goodsInfo,vo);
          arrVO.add(vo);
      }
      return arrVO;
    }

    @Transactional(rollbackFor = Exception.class)
    public PageVO search(GoodsInfoSearch goodsInfoSearch) {

        //第1页，这个里面必须传0 ,理解为PageHelper.startPage()
        Pageable pageable= PageRequest.of( goodsInfoSearch.getPage()-1, goodsInfoSearch.getSize());

        //构造分对象
        Page<GoodsInfo> page = this.goodsInfoRepository.findAll(new GoodsInfoSearchSpecification(goodsInfoSearch), pageable);

        PageVO  pageVO=new PageVO();

        pageVO.setTotal( page.getTotalElements());

        List<GoodsInfoVO> listVO=new ArrayList<>();
        for(GoodsInfo entity: page.getContent()){
            GoodsInfoVO  vo=new GoodsInfoVO();
            BeanUtils.copyProperties(entity,vo);
            listVO.add(vo);
        }
        pageVO.setRows(listVO);
        return pageVO;

    }

    /**
     *  自定义查询条件
     */
    class GoodsInfoSearchSpecification implements Specification<GoodsInfo>{

        private GoodsInfoSearch  goodsInfoSearch;

        public GoodsInfoSearchSpecification(GoodsInfoSearch goodsInfoSearch){
            this.goodsInfoSearch=goodsInfoSearch;
        }

        @Override
        public Predicate toPredicate(Root<GoodsInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            //定义查询表达式
            List<Predicate> lstPredicates = new ArrayList<Predicate>();

            //动态sql
            if( this.goodsInfoSearch.getName()!=null && this.goodsInfoSearch.getName()!=""){
                Predicate goodsName = criteriaBuilder.like(root.get("goodsName"), "%" + this.goodsInfoSearch.getName() + "%");
                lstPredicates.add(goodsName);
            }

            if(this.goodsInfoSearch.getCategoryId()!=null){
                Predicate goodsCategoryId = criteriaBuilder.equal(root.get("goodsCategoryId"), this.goodsInfoSearch.getCategoryId());
                lstPredicates.add(goodsCategoryId);
            }


            //把list--> array
            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
